<template>
    <!--地图挂载dom-->
    <div id="map">
    </div>
</template>

<script>
    //引入依赖
    import GML from 'ol/format/GML';
    import Map from 'ol/Map';
    import VectorSource from 'ol/source/Vector';
    import View from 'ol/View';
    import OSM from 'ol/source/OSM'
    import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
    import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
    export default {
        name: 'GmlLayer',
        data() {
            return {
            }
        },
        mounted() {

            const raster = new TileLayer({
                source: new OSM()//加载OpenStreetMap
            });

            const style = {
                'Point': new Style({
                    image: new CircleStyle({
                        fill: new Fill({
                            color: 'rgba(255,255,0,0.4)',
                        }),
                        radius: 5,
                        stroke: new Stroke({
                            color: '#ff0',
                            width: 1,
                        }),
                    }),
                }),
                'LineString': new Style({
                    stroke: new Stroke({
                        color: '#f00',
                        width: 3,
                    }),
                }),
                'MultiLineString': new Style({
                    stroke: new Stroke({
                        color: '#0f0',
                        width: 3,
                    }),
                }),
            };

            //同样需要使用矢量数据源与矢量图层加载
            const vector = new VectorLayer({
                source: new VectorSource({
                    url: 'http://localhost:8888/openlayer/gml/1.gml',
                    format: new GML(),//指定gml数据格式
                }),
                style: function (feature) {
                    return style[feature.getGeometry().getType()];
                },
            });
            const map = new Map({
                layers: [raster, vector],
                target: document.getElementById('map'),
                view: new View({
                    center: [106.432666551,35.354313681],
                    zoom: 18,
                    projection: "EPSG:4326", //指定投影
                })
            });
            let source = vector.getSource();
            let features = source.getFeatures();
            map.getView().fit(features[0].getGeometry())
        },
        methods: {
        }
    }
</script>

<style scoped>
    #map {
        width: 100%;
        height: 100%;
    }

    .MapTool {
        position: absolute;
        top: .5em;
        right: .5em;
        z-index: 9999;
    }
</style>
